<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class record {

	//提交操作流水记录
	public function submitRecord() {
		if (empty(getPost('operatetype')) || valid::isWicked(getPost('operatetype'))) {
			return util::getValidError('操作类型格式异常');
		}

		if (empty(getPost('module')) || valid::isWicked(getPost('module'))) {
			return util::getValidError('操作模块格式异常');
		}

		if (empty(getPost('content')) || valid::isWicked(getPost('content'))) {
			return util::getValidError('操作内容格式异常');
		}

		if (getPost('storage') != '0' && getPost('storage') != '1') {
			return util::getValidError('存储方式格式异常');
		}

		$rst = new sqlcon();
		$uuid = util::getUUID();
		$contentPath = '';

		//保存较长的关联数据
		if (getPost('storage') == '1' && !empty(getPost('association'))) {
			$contentPath = 'upload/record/' . $uuid . '.txt';
			$contentFile = $uuid . '.txt';
			util::writeFile('upload/record', $contentFile, getPost('association'));
		}

		//获取操作者的编号与姓名
		$token = util::decrypt(getServer('HTTP_TOKEN'));
		$temp = explode('[@#%&]', $token);
		$userid = $temp[0];
		$name = $temp[3];

		//若有代班情况，则获取被代班者的编号与姓名
		$dutyToken = getServer('HTTP_DUTY');
		$isduty = 0;
		$dutyerid = '';
		$dutyer = '';
		if (!empty($dutyToken) && $dutyToken != 'null') {
			$isduty = 1;
			$dutyToken = util::decrypt($dutyToken);
			$temp = explode('[@#%&]', $dutyToken);
			$dutyerid = $temp[0];
			$dutyer = $temp[1];
		}

		//保存操作流水信息
		$rst->addCollect('recordid', $uuid);
		$rst->addCollect('type', getPost('operatetype'));
		$rst->addCollect('module', getPost('module'));
		$rst->addCollect('content', getPost('content'));
		$rst->addCollect('association', getPost('storage') == '0' ? getPost('association') : $contentPath);
		$rst->addCollect('storage', getPost('storage'));
		$rst->addCollect('operaterid', $userid);
		$rst->addCollect('operater', $name);
		$rst->addCollect('operatetime', date('Y-m-d H:i:s'));
		$rst->addCollect('isduty', $isduty);
		$rst->addCollect('dutyerid', $dutyerid);
		$rst->addCollect('dutyer', $dutyer);
		$rst->addCollect('remark', getPost('remark'));
		$rst->insertInfo('record');

		return util::getSuccess('success');
	}

	//获取操作模块列表
	public function getModuleList() {
		$rst = new sqlcon();
		$list = $rst->getList('distinct module as text, module as value', 'record', 'id != 0');

		$result = util::getSuccess('success');
		$result['moduleList'] = $list;

		return $result;
	}

	//获取操作人员列表
	public function getWorkerList() {
		$rst = new sqlcon();
		$list = $rst->getList('name as text, name as value', 'user', 'isuse = 1');

		$result = util::getSuccess('success');
		$result['workerList'] = $list;

		return $result;
	}

	//获取操作流水列表
	public function getFlowlogList() {
		$module = getPost('moduleInfo');
		$worker = getPost('workerInfo');
		$type = getPost('typeInfo');
		$startdate = getPost('startdate');
		$enddate = getPost('enddate');
		$page = getPost('page', '1');
		$permission = getPost('permission');

		$queryitem = '';
		if ($module != '')
			$queryitem .= " and module = '" . $module . "'";
		if ($worker != '')
			$queryitem .= " and operater = '" . $worker . "'";
		if ($type != '')
			$queryitem .= " and type = '" . $type . "'";
		if ($startdate != '')
			$queryitem .= " and operatetime >= '" . $startdate . "'";
		if ($enddate != '')
			$queryitem .= " and operatetime <= '" . $enddate . " 23:59:59'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';
		$sortOption = 'operatetime desc, id desc';

		$rst = new sqlcon();

		$info = $rst->getInfo('count(*) as num', 'record', 'id != 0' . $queryitem);

		$list = $rst->getList('recordid, module, content, association, operater, operatetime', 'record', 'id != 0' . $queryitem, $sortOption . $limititem);

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['flowlog'] = $list;
	    $result['rowCount'] = 50;
	    if ($permission == 'yes') {
	    	$result['permission'] = util::getOperateItem(getPost('menuid'));
	    }
		return $result;
	}

	//删除一条操作流水日志
	public function deleteFlowlog() {
		if (empty(getPost('recordid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();

		$rst->addCollect('recordid', getPost('recordid'));
        $rst->deleteInfo('record');

        return util::getSuccess('success');
	}

	//删除多条操作流水日志
	public function deleteMoreFlowlog() {
		if (empty(getPost('tempid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();
		$idlist = explode('■', getPost('tempid'));

		for ($i = 0; $i < count($idlist) - 1; $i++) {
			$rst->addCollect('recordid', $idlist[$i]);
			$rst->deleteInfo('record');
		}

		return util::getSuccess('success');
	}

	//获取相关联的操作日志
	public function getAssociationLog() {
		if (empty(getPost('association'))) {
			return util::getValidError('关联ID格式错误');
		}

		$rst = new sqlcon();

		$list = $rst->getList('module, content, operater, operatetime', 'record', "association = '" . getPost('association') . "'", 'operatetime asc');

		$result = util::getSuccess('success');
		$result['logList'] = $list;
		return $result;
	}

	//获取举报信息列表
	public function getReportList() {
		$reason = getPost('reasonInfo');
		$status = getPost('statusInfo');
		$startdate = getPost('startdate');
		$enddate = getPost('enddate');
		$page = getPost('page', '1');

		$queryitem = '';
		if ($reason != '')
			$queryitem .= " and reason = '" . $reason . "'";
		if ($status != '')
			$queryitem .= ' and status = ' . $status;
		if ($startdate != '')
			$queryitem .= " and reporttime >= '" . $startdate . "'";
		if ($enddate != '')
			$queryitem .= " and reporttime <= '" . $enddate . " 23:59:59'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';
		$sortOption = 'reporttime desc, id desc';

		$rst = new sqlcon();

		$info = $rst->getInfo('count(*) as num', 'report', 'id != 0' . $queryitem);

		$list = $rst->getList('reportid, title, reason, details, person, phone, association, module, reporttime, status, result', 'report', 'id != 0' . $queryitem, $sortOption . $limititem);

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['report'] = $list;
	    $result['rowCount'] = 50;
		return $result;
	}

	//处理举报信息
	public function handleReport() {
		if (empty(getPost('reportid'))) {
			return util::getValidError('ID格式错误');
		}

		if (empty(getPost('result')) || valid::isWicked(getPost('result'))) {
			return util::getValidError('处理结果格式错误');
		}

		$rst = new sqlcon();
		$rst->addCollect('status', '1');
		$rst->addCollect('result', getPost('result'));
		$rst->addCollect('reportid', getPost('reportid'));
		$rst->updateInfo('report');

		//获取待处理的举报信息数量
		$info = $rst->getInfo('count(*) as num', 'report', "status = 0");

		$result = util::getSuccess('success');
		$result['waitReportCount'] = $info['num'];
		return $result;
	}

	//获取自动化日志的筛选项
	public function getAutologOptionList() {
		$rst = new sqlcon();

		$moduleList = $rst->getList('distinct module as text, module as value', 'autolog', 'id != 0');
		$operaterList = $rst->getList('name as text, name as value', 'user', 'isuse = 1');

		$result = util::getSuccess('success');
		$result['moduleList'] = $moduleList;
		$result['operaterList'] = $operaterList;
		return $result;
	}

	//获取自动化日志列表
	public function getAutologList() {
		$channel = getPost('channelInfo');
		$module = getPost('moduleInfo');
		$operater = getPost('operaterInfo');
		$status = getPost('statusInfo');
		$startdate = getPost('startdate');
		$enddate = getPost('enddate');
		$page = getPost('page', '1');
		$permission = getPost('permission');

		$queryitem = '';
		if ($channel != '')
			$queryitem .= ' and channel = ' . $channel;
		if ($module != '')
			$queryitem .= " and module = '" . $module . "'";
		if ($operater != '')
			$queryitem .= " and operater = '" . $operater . "'";
		if ($status != '')
			$queryitem .= ' and status = ' . $status;
		if ($startdate != '')
			$queryitem .= " and starttime >= '" . $startdate . "'";
		if ($enddate != '')
			$queryitem .= " and starttime <= '" . $enddate . " 23:59:59'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';
		$sortOption = 'starttime desc, id desc';

		$rst = new sqlcon();

		$info = $rst->getInfo('count(*) as num', 'autolog', 'id != 0' . $queryitem);

		$list = $rst->getList('logid, channel, module, operater, autoer, status, result, starttime, endtime', 'autolog', 'id != 0' . $queryitem, $sortOption . $limititem);

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['autolog'] = $list;
	    $result['rowCount'] = 50;
	    if ($permission == 'yes') {
	    	$result['permission'] = util::getOperateItem(getPost('menuid'));
	    }
		return $result;
	}

	//强制中止自动化的执行
	public function stopAutolog() {
		if (empty(getPost('logid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();

		$rst->addCollect('status', '3');
		$rst->addCollect('result', '执行异常，被中止');
		$rst->addCollect('endtime', date('Y-m-d H:i:s'));
		$rst->addCollect('logid', getPost('logid'));
        $rst->updateInfo('autolog');

        return util::getSuccess('success');
	}

	//标识已修复的自动化日志
	public function repairAutolog() {
		if (empty(getPost('logid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();

		$rst->addCollect('status', '4');
		$rst->addCollect('result', '问题已修复');
		$rst->addCollect('logid', getPost('logid'));
        $rst->updateInfo('autolog');

        return util::getSuccess('success');
	}

	//删除一条自动化日志
	public function deleteAutolog() {
		if (empty(getPost('logid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();

		//删除日志的内容文件
		$file_path = _FILE_PATH_ . 'upload/autolog/' . getPost('logid') . '.txt';
		if (file_exists($file_path)) {
			unlink($file_path);
		}

		$rst->addCollect('logid', getPost('logid'));
        $rst->deleteInfo('autolog');

        return util::getSuccess('success');
	}

	//删除多条自动化日志
	public function deleteMoreAutolog() {
		if (empty(getPost('tempid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();
		$idlist = explode('■', getPost('tempid'));

		for ($i = 0; $i < count($idlist) - 1; $i++) {
			//删除日志的内容文件
			$file_path = _FILE_PATH_ . 'upload/autolog/' . $idlist[$i] . '.txt';
			if (file_exists($file_path)) {
				unlink($file_path);
			}
			
			$rst->addCollect('logid', $idlist[$i]);
			$rst->deleteInfo('autolog');
		}

		return util::getSuccess('success');
	}

	//获取自动化日志内容
	public function getAutologInfo() {
		if (empty(getPost('logid'))) {
			return util::getValidError('日志ID格式错误');
		}

		$rst = new sqlcon();

		//获取日志信息
        $info = $rst->getInfo('logid', 'autolog', "logid = '" . getGet('logid') . "'");

		//获取日志内容
        $file_path = _FILE_PATH_ . 'upload/autolog/' . getPost('logid') . '.txt';
        if (file_exists($file_path)) {
        	$info['html'] = nl2br(util::readFile($file_path));
        }
        else {
        	$info['html'] = '日志内容不存在！';
        }

        //高亮显示关键词
        if (!empty($info['html']) && $info['html'] != '日志内容不存在！') {
        	$errorList = array('程序有误', '没有返回', '没有进入', '异常', '未登录成功', 'ERROR');
	        foreach ($errorList as $error) {
				if (strpos($info['html'], $error) !== false) {
					$info['html'] = str_replace($error, '<span class="tc1">' . $error . '</span>', $info['html']);
				}
			}

			$successList = array('提交成功');
	        foreach ($successList as $success) {
				if (strpos($info['html'], $success) !== false) {
					$info['html'] = str_replace($success, '<span class="tc4">' . $success . '</span>', $info['html']);
				}
			}
        }
		
		$result = util::getSuccess('success');
		$result['autologInfo'] = $info;
		return $result;
	}
	
}